<?php

/*
	Data Access Object for Team module
*/

/*require_once(FWS_DIR_MODEL.'teammodel.class.php');*/

class TeamModelDao {

	function findAllTeam() {
       	$resultat = mysql_query("SELECT ID_TEAM, LIB_TEAM, COD_TEAM, ID_CLUB, MANAGED FROM TBL_TEAM ORDER BY LIB_TEAM") or trigger_error(mysql_error(), E_USER_ERROR);
		$result = array();
       	while($row = mysql_fetch_array($resultat)) {
			$result[] = new TeamModel($row);
   		}
		return $result;
	}
	
	function findAllTeamManaged() {
       	$resultat = mysql_query("SELECT ID_TEAM, LIB_TEAM, COD_TEAM, ID_CLUB, MANAGED FROM TBL_TEAM WHERE MANAGED=1") or trigger_error(mysql_error(), E_USER_ERROR);
		$result = array();
       	while($row = mysql_fetch_array($resultat)) {
			$result[$row[0]] = $row[1]; 
   		}
		return $result;
	}
	
	
	function findAllTeamForEvent($id_event) {
		$query = "SELECT T.LIB_TEAM FROM EVENT_HAS_TEAM ET INNER JOIN TBL_TEAM T ON ET.ID_TEAM=T.ID_TEAM WHERE T.MANAGED=1 AND ET.ID_EVENT=$id_event";
		$resultat = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
		$result = array();
       	while($row = mysql_fetch_array($resultat)) {
			$result[] = $row[0]; 
   		}
		return $result;
	}
	
	
	function findRefTeam() {
       	$resultat = mysql_query("SELECT ID_TEAM, LIB_TEAM FROM TBL_TEAM ORDER BY LIB_TEAM") or trigger_error(mysql_error(), E_USER_ERROR);
		$result = array();
       	while($row = mysql_fetch_array($resultat)) {
			$result[$row[0]] = $row[1]; 
   		}
		return $result;
	}

	function queryTeamById($id_Team) {
		$query  = "SELECT T.ID_TEAM, T.LIB_TEAM, T.COD_TEAM, T.MANAGED, T.ID_CLUB, C.LIB_CLUB ";
		$query .= "FROM TBL_TEAM T LEFT OUTER JOIN TBL_CLUB C ON T.ID_CLUB = C.ID_CLUB ";
		$query .= "WHERE T.ID_TEAM=".$id_Team;
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		$row = mysql_fetch_assoc($result);
		$Team = new TeamModel($row); 
		return $Team;
	}
	
	function updateTeam($Team) {
		$Team = array();
		$query  = "UPDATE TBL_TEAM SET ";
		$query .= "LIB_TEAM='".$Team->getLibTeam();
		$query .= "', COD_TEAM='".$Team->getCodTeam();
		$query .= "', ID_CLUB='".$Team->getIdClub();
		$query .= "' WHERE ID_TEAM=".$Team->getIdTeam();
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
	}
	
	function insertTeam($Team) {
		$Team = array();
		$query  = "INSERT INTO TBL_TEAM(LIB_TEAM,";
		$query .= "COD_TEAM, ID_CLUB)";
		$query .= "VALUES('".$Team->getLibTeam()."', '";
		$query .= $Team->getSaison()."' ";
		$query .= $Team->getIdClub()."')";
		
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		
	}

}
?>